package com.juanLL.CMC;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.generico.frame.Music;
import com.generico.frame.Sound;
import com.generico.frame.impl.GLGame;

/**
 * 
 * AssetLoader class, provides resources from the assets folder.
 * 
 */
public class AssetLoader {
	// Folder path to bitmap resources
	public static final String TRASH_PATH = "bitmap/trash/";
	public static final String SPLASH_PATH = "bitmap/splash/";
	public static final String BACKGROUND_PATH = "bitmap/background/";
	public static final String RECYCLEBIN_PATH = "bitmap/recyclebin/";
	public static final String CITIZEN_PATH = "bitmap/citizen/";
	public static final String RAT_PATH = "bitmap/rat/rat.png";
	// File path to specific bitmap files
	public static final String PATH_TRASH_OAPPLE = TRASH_PATH + "oApple.png";
	public static final String PATH_TRASH_OBANANA = TRASH_PATH + "oBanana.png";
	public static final String PATH_TRASH_PBAG = TRASH_PATH + "pBag.png";
	public static final String PATH_TRASH_PBOTTLE = TRASH_PATH + "pBottle.png";
	public static final String PATH_TRASH_GBOTTLE = TRASH_PATH + "gBottle.png";
	public static final String PATH_TRASH_GJAR = TRASH_PATH + "gJar.png";
	public static final String PATH_RECYCLEBIN_ORGANIC = RECYCLEBIN_PATH
			+ "oBin.png";
	public static final String PATH_RECYCLEBIN_PLASTIC = RECYCLEBIN_PATH
			+ "pBin.png";
	public static final String PATH_RECYCLEBIN_GLASS = RECYCLEBIN_PATH
			+ "gBin.png";
	public static final String PATH_CITIZEN_MAN1 = CITIZEN_PATH + "man1.png";
	public static final String PATH_BACKGROUND_CITY1 = BACKGROUND_PATH
			+ "city1.jpg";
	public static final String PATH_BACKGROUND_CITY2 = BACKGROUND_PATH
			+ "city2.jpg";
	public static final String PATH_SPLASH_1 = SPLASH_PATH + "city1.jpg";
	public static final String PATH_SPLASH_2 = SPLASH_PATH + "city2.jpg";
	public static final String PATH_SPLASH_3 = SPLASH_PATH + "thanks.jpg";
	public static final String PATH_PAUSE_BUTTON = "bitmap/pause.png";
	// Log Tag for debugging
	public static final String LOG_TAG = "AssetLoader";
	// AssetManager that provides access to the files
	AssetManager assetMger;
	// input stream that pulls files into a stream for decoding
	InputStream iS;

	// Musica de fondo
	public static Music music;
	// Para los sonidos individuales
	public static Sound sonido1;

	public static void load(GLGame game) {
		music = game.getAudio().newMusic("sounds/background.ogg");
		music.setLooping(true);
		music.setVolume(0.5f);
		music.play();
	}

	/**
	 * AssetLoader constructor
	 * 
	 * @param context
	 */
	public AssetLoader(Context context, GLGame game) {
		assetMger = context.getAssets();
		music = game.getAudio().newMusic("sounds/background.ogg");
		music.setLooping(true);
		music.setVolume(0.5f);
	}

	public AssetLoader(Context context) {
		// TODO Auto-generated constructor stub
		assetMger = context.getAssets();
		// music = game.getAudio().newMusic("background.ogg");
		// music.setLooping(true);
		// music.setVolume(0.5f);
	}

	/**
	 * loadBitmap, loads a bitmap from the assets folder and returns it
	 * 
	 * @param path
	 *            the complete file path
	 * @return Bitmap, the specified bitmap
	 */
	public Bitmap loadBitmap(String path) {
		Bitmap bitmap;
		try {
			iS = assetMger.open(path);
			bitmap = BitmapFactory.decodeStream(iS);
			iS.close();
			return bitmap;
		} catch (IOException e) {
			Log.d(AssetLoader.LOG_TAG, e.toString());
		}

		return null;
	}

	public AssetFileDescriptor getAFD(String filename) {
		try {
			return assetMger.openFd(filename);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}
